#!/bin/bash

#usage: ./setup.sh <test config file>

# flags for scp
scp_flags=""

# load the config
config=$1
source $config

./export.sh  $config

# some convenience vars
x509ac="/home/ashish/Thesis/x509ac"
hipl_conf_dir="/usr/local/etc/hip/"

echo "Inititator:  " $initiator
echo "Responder:   " $responder
echo "Middlebox 1: " $mbox1
echo "Middlebox 2: " $mbox2

# put binaries in place
echo -n "Putting hipd and hipfw binaries in place... "
echo -n "Placing 64 bit binaries in the initiator"
ssh -p $initiator_port root@$initiator "rm -f $hipldir_remote/hipd $hipldir_remote/hipfw"
scp -P $initiator_port $scp_flags ${binary_64}/hipd		root@${initiator}:${hipldir_remote}
scp -P $initiator_port $scp_flags ${binary_64}/auth_performance	root@${initiator}:${hipldir_remote}
scp -P $initiator_port $scp_flags ${binary_64}/hipfw		root@${initiator}:${hipldir_remote}
scp -P $initiator_port $scp_flags client			root@${initiator}:${hipldir_remote}

echo -n "Placing 64 bit binaries in the responder"
ssh -p $responder_port root@$responder "rm -f $hipldir_remote/hipd $hipldir_remote/hipfw"
scp -P $responder_port $scp_flags ${binary_64}/hipd		root@${responder}:${hipldir_remote}
scp -P $responder_port $scp_flags ${binary_64}/auth_performance	root@${responder}:${hipldir_remote}
scp -P $responder_port $scp_flags ${binary_64}/hipfw		root@${responder}:${hipldir_remote}
scp -P $responder_port $scp_flags server			root@${responder}:${hipldir_remote}

echo -n "Placing 32 bit binaries in the Mbox1"
ssh -p $mbox1_port root@$mbox1 "rm -f $hipldir_remote/hipd $hipldir_remote/hipfw"
scp -P $mbox1_port $scp_flags ${binary_32}/hipfw  root@${mbox1}:${hipldir_remote}
#scp -P $mbox1_port $scp_flags ${binary_64}/hipfw  root@${mbox1}:${hipldir_remote}

echo -n "Placing 32 bit binaries in the Mbox2"
ssh -p $mbox2_port root@$mbox2 "rm -f $hipldir_remote/hipd $hipldir_remote/hipfw"
scp -P $mbox2_port $scp_flags ${binary_32}/hipfw  root@${mbox2}:${hipldir_remote}
#scp -P $mbox2_port $scp_flags ${binary_64}/hipfw  root@${mbox2}:${hipldir_remote}

echo "done."
echo "ATTENTION: did not update binaries on middleboxes"

# put ac lib in place
echo -n "Putting x509ac lib in place... "
#scp $scp_flags ${x509ac}/libx509ac* root@${initiator}:/usr/lib/
#scp $scp_flags ${x509ac}/libx509ac* root@${responder}:/usr/lib/
#scp $scp_flags ${x509ac}/libx509ac* root@${mbox1}:/usr/lib/
#scp $scp_flags ${x509ac}/libx509ac* root@${mbox2}:/usr/lib/
echo "done."

# put keys and configs in place
echo -n "Creating hip config dir... "
ssh -p $initiator_port root@${initiator} "rm -rf $hipl_conf_dir; mkdir $hipl_conf_dir; mkdir $hipl_conf_dir/user_certchains/; mkdir $hipl_conf_dir/trusted_certs/"
ssh -p $responder_port root@${responder} "rm -rf $hipl_conf_dir; mkdir $hipl_conf_dir; mkdir $hipl_conf_dir/user_certchains/; mkdir $hipl_conf_dir/trusted_certs/"
ssh -p $mbox1_port root@${mbox1}     "rm -rf $hipl_conf_dir; mkdir $hipl_conf_dir; mkdir $hipl_conf_dir/user_certchains/; mkdir $hipl_conf_dir/trusted_certs/"
ssh -p $mbox2_port root@${mbox2}     "rm -rf $hipl_conf_dir; mkdir $hipl_conf_dir; mkdir $hipl_conf_dir/user_certchains/; mkdir $hipl_conf_dir/trusted_certs/"
echo "done."

echo -n "Putting hip config in place..."
scp -P $initiator_port $scp_flags $initiator_hip_cfg/* root@${initiator}:$hipl_conf_dir
scp -P $responder_port $scp_flags $responder_hip_cfg/* root@${responder}:$hipl_conf_dir
scp -P $mbox1_port  $scp_flags $mbox_hip_cfg/*      root@${mbox1}:$hipl_conf_dir
scp -P $mbox2_port  $scp_flags $mbox2_hip_cfg/*      root@${mbox2}:$hipl_conf_dir
echo "done."

echo -n "Putting firewall configs in place... "
scp -P $initiator_port $scp_flags $initiator_hipfw_cfg       root@${initiator}:$hipl_conf_dir/signaling_firewall_policy.cfg
scp -P $initiator_port $scp_flags $initiator_hipfw_cfg       root@${initiator}:$hipl_conf_dir/signaling_local_firewall_policy.cfg
scp -P $responder_port $scp_flags $responder_hipfw_cfg       root@${responder}:$hipl_conf_dir/signaling_firewall_policy.cfg
scp -P $responder_port $scp_flags $responder_hipfw_cfg       root@${responder}:$hipl_conf_dir/signaling_local_firewall_policy.cfg
scp -P $mbox1_port  $scp_flags $mbox_hipfw_cfg   root@${mbox1}:$hipl_conf_dir/signaling_firewall_policy.cfg
scp -P $mbox2_port  $scp_flags $mbox2_hipfw_cfg  root@${mbox2}:$hipl_conf_dir/signaling_firewall_policy.cfg
echo "done."

echo -n "Putting user keys in place... "
scp -P $initiator_port $scp_flags ${user_key_dir}/initiator-user-key.pem   root@${initiator}:$hipldir_remote/.signaling/user-key.pem
scp -P $initiator_port $scp_flags ${user_key_dir}/initiator-user-cert.pem   root@${initiator}:$hipldir_remote/.signaling/user-cert-chain.pem
scp -P $responder_port $scp_flags ${user_key_dir}/responder-user-key.pem   root@${responder}:$hipldir_remote/.signaling/user-key.pem
scp -P $responder_port $scp_flags ${user_key_dir}/responder-user-cert.pem   root@${responder}:$hipldir_remote/.signaling/user-cert-chain.pem
echo "done."

# function to create tars of certificate archives
# arg1: the folder which contents should be tared
# arg2: the file where to put the archive
function tar_certs() {
	dir=$1
	destfile=$2
	curdir=$PWD
	cd $dir
	tar -czf $destfile *
	cd $curdir
}

# function to setup certificates
# arg1: the destination machine where to setup certificates
function setup_user_certs() {
	dest=$1
    destport=$2
	tar_certs $user_certificate_dir $PWD/user_certs.tar
	scp -P $destport $scp_flags user_certs.tar  root@${dest}:$hipl_conf_dir
	rm user_certs.tar
	ssh -p $destport root@$dest "cd $hipl_conf_dir/user_certchains/; tar -xzf ../user_certs.tar; cd ..; rm user_certs.tar"
}

# arg1: the destination machine where to setup certificates
function setup_trusted_certs() {
	dest=$1
    destport=$2
	tar_certs $trusted_certificate_dir $PWD/tr_certs.tar
	scp -P $destport $scp_flags tr_certs.tar    root@${dest}:$hipl_conf_dir
	rm tr_certs.tar
	ssh -p $destport root@$dest "cd $hipl_conf_dir/trusted_certs/; tar -xzf ../tr_certs.tar; cd ..; rm tr_certs.tar"
}

# put trusted certificates in place
echo -n "Putting trust roots in place... "
setup_trusted_certs $initiator $initiator_port
setup_trusted_certs $responder $responder_port
setup_trusted_certs $mbox1 $mbox1_port
setup_trusted_certs $mbox2 $mbox2_port  
echo "done."

# put application certificates in place or flush them
if [ $app_certificates -eq 1 ]; then	
    echo -n "Putting application certificates in place... "
    scp -P $initiator_port $scp_flags $app_cert_client  root@$initiator:$app_cert_dir_client/client.cert
    scp -P $initiator_port $scp_flags $app_chain_client root@$initiator:$app_cert_dir_client/client.chain
	scp -P $responder_port $scp_flags $app_cert_server root@$responder:$app_cert_dir_server/server.cert
	scp -P $responder_port $scp_flags $app_chain_server root@$responder:$app_cert_dir_server/server.chain
    echo "done."
else
	echo -n "Flushing application certificates... "
    c1=$(basename $app_cert_client)
    c2=$(basename $app_chain_client)
    s1=$(basename $app_cert_server)
    s2=$(basename $app_chain_server)
	ssh -p $initiator_port root@$initiator "rm $app_cert_dir_client/$c1 $app_cert_dir_client/$c2"
	ssh -p $responder_port root@$responder "rm $app_cert_dir_server/$s1 $app_cert_dir_server/$s2"
    echo "done."
fi

# put user certificates in place or flush them
if [ $user_certificates -eq 1 ]; then	
    echo "Putting user certificates in place... "
	setup_user_certs $initiator $initiator_port
	setup_user_certs $responder $responder_port
	setup_user_certs $mbox1 $mbox1_port
	setup_user_certs $mbox2 $mbox2_port
    echo "done."
else
	echo "Flushing all user certificates... "
	ssh -p $initiator_port root@$initiator "rm -rf $hipl_conf_dir/user_certchains/*"
	ssh -p $responder_port root@$responder "rm -rf $hipl_conf_dir/user_certchains/*"
	ssh -p $mbox1_port root@$mbox1     "rm -rf $hipl_conf_dir/user_certchains/*"
	ssh -p $mbox2_port root@$mbox2     "rm -rf $hipl_conf_dir/user_certchains/*"
    echo "done."
fi

#if [ -n $custom_setup ]; then
#    echo "Executing custom setup script: " $custom_setup
#    $custom_setup $config
#fi
